define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Wenda, $ionicSlideBoxDelegate,GoPage,GoDialog,$timeout,$ionicScrollDelegate) {
		
		$scope.selectIndex = 0;
		$scope.datasArr = new Array();//存放一览数据
		$scope.nPageArr = [1,1];//页数
		$scope.input = new Object();
		$scope.input.reply_content = "";//回复
		
		//画面加载
		$scope.$on('$ionicView.beforeEnter', function(scope, status) {
			/*
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				lan = 20;
			}
			var all = window.innerHeight;
//			$scope.myObj = {
//				"height": all - 44*2 - lan + "px"
//			}

			var barheader = document.getElementsByClassName("bar-header");//头部标题栏
			var tabH = 44;//tab高度
			if(barheader.length>0){
				topH = barheader[barheader.length-1].offsetHeight;
			}
			//$scope.allHeight = all - 44*2 - lan + "px";
			$scope.allHeight = all - topH - tabH + "px"
			
			//动态设置高度
			GoPage.init_top($scope, 44,30,44);
			$scope.init_data();
			*/
		}, false);
		$scope.$on('$ionicView.afterEnter', function() {
			var barheader = document.getElementsByClassName("bar-header");//头部标题栏
			var topH = 44;//标题高度
			var tabH = 44;//tab高度
			
			if(barheader.length>0){
				topH = barheader[barheader.length-1].offsetHeight;
			}
			var all = window.innerHeight;
			//$scope.allHeight = all - 44*2 - lan + "px";
			$scope.allHeight = all - topH - tabH + "px"
			
			//动态设置高度
			GoPage.init_top($scope, 44,30,44);
			$scope.init_data();
		}, false);
		//切换页面
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide($scope.selectIndex);
			if(!$scope.datasArr[$scope.selectIndex]){
				$scope.init_data();
			}
		};
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		
		//初始化。status=1未回复  status=2已回复
		$scope.init_data = function(){
			Wenda.getList($scope.selectIndex+1,1,function(data){
				$scope.datasArr[$scope.selectIndex] = data;
				document.getElementById("wait").style.height = $scope.allHeight;//必须这样设置高度。如果使用ng-style="myObj" ，当改变datasArr里的内容时，滚动不了
				document.getElementById("already").style.height = $scope.allHeight;
			})
		}
		
		//显示回复
		$scope.showReply = function(index){
			var datas = $scope.datasArr[$scope.selectIndex].consult;
			for (var i=0;i<datas.length;i++) {//隐藏其他
				if(i!=index){
					datas[i].isShowReply = false;
				}
			}
			datas[index].isShowReply = !datas[index].isShowReply;
		}
		
		//显示回复输入框
		$scope.showReplyInput = function(index){
			$scope.input.reply_content = "";
			var datas = $scope.datasArr[$scope.selectIndex].consult;
			for (var i=0;i<datas.length;i++) {//隐藏其他
				if(i!=index){
					datas[i].isShowReplyInput = false;
				}
			}
			datas[index].isShowReplyInput = !datas[index].isShowReplyInput;
			$scope.comment_id = datas[index].comment_id;
		}
		
		//提交回复
		$scope.submit = function(){
			if(!$scope.input.reply_content){
				GoDialog.toast("请输入回复的内容");
				return;
			}
			Wenda.answer($scope.comment_id,$scope.input.reply_content,function(data){
				if(data.success){
					GoDialog.toast("回复成功");
					$scope.datasArr[1] = null;//初始化。目的：当从第一个tab切换到第二个时，第二个会再次加载
					$scope.init_data();
				}else{
					GoDialog.alert("提示",data.msg);
				}
			})
		}
		
		//删除
		$scope.deleteWenDa = function(comment_id){
			GoDialog.confirm("提示","确定要删除吗？",function(){
				Wenda.deleteWenDa(comment_id,function(data){
					if(data.success){
						GoDialog.toast("删除成功");
						$scope.init_data();
					}else{
						GoDialog.alert("提示",data.msg);
					}
				})
			})
		}
		
		
		/*===================下拉刷新 =============*/
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Wenda.getList($scope.selectIndex+1, 1, function(data) {
				$scope.datasArr[$scope.selectIndex] = data;
				$scope.nPageArr[$scope.selectIndex] = 1;
			},function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Wenda.getList($scope.selectIndex+1, $scope.nPageArr[$scope.selectIndex]+1, function(data) {
//				console.log(data.consult)
					if(data != null) {
						if($scope.datasArr[$scope.selectIndex] != undefined) {
							$scope.datasArr[$scope.selectIndex].consult = $scope.datasArr[$scope.selectIndex].consult.concat(data.consult);
							$scope.nPageArr[$scope.selectIndex] += 1;
//							console.log($scope.nPageArr)
						} else {
							$scope.datasArr[$scope.selectIndex] = data;
						}
					}
				},function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		}
		/*==================下拉刷新 =============*/


	}

	controller.$inject = ['$scope', 'Wenda', '$ionicSlideBoxDelegate', 'GoPage','GoDialog','$timeout','$ionicScrollDelegate'];

	return app.registerController("wendactrl",controller);
});